package demo.hdz.mynotification;

import android.app.NotificationManager;
import android.app.RemoteInput;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.RequiresApi;
import android.util.Log;

/**
 * @author hdz
 */
public class NotificationService extends Service {

    private static final String TAG = "NotificationService";
    private NotificationManager mNM;
    @Override
    public void onCreate() {
        super.onCreate();
        mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && intent.getAction() != null) {
            Logger.i(TAG, "onStartCommand action = " + intent.getAction());
            switch (intent.getAction()) {
                case Constant.ACTION_SIMPLE:
                    Logger.d("======");
                    break;
                case Constant.ACTION_ACTION:
                    Logger.d("带Action动作按钮的通知");
                    break;
                case Constant.ACTION_NO:
                    Logger.d("======删除通知");
                    mNM.cancel(Notifications.NOTIFICATION_ACTION);
                    break;
                case Constant.ACTION_CUSTOM_HEADS_UP_VIEW:
                    dealWithActionCustomHeadsUpView(intent);
                    break;
                case Constant.ACTION_SEND_PROGRESS_NOTIFICATION:
                    dealWithActionSendProgressNotification();
                    break;
                case Constant.ACTION_REMOTE_INPUT:
                    Logger.d("==点击快速回复通知！");
                    break;
                case Constant.ACTION_REPLY:
                    Logger.d("==快速回复");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                        dealWithActionReplay(intent);
                    }
                    break;
                case Constant.ACTION_BIG_PICTURE_STYLE:
                    Logger.d("==大图通知");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_LOVE:
                    Logger.d("++喜欢");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_PRE:
                    Logger.d("++上一首");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_PLAY_OR_PAUSE:
                    Logger.d("++播放/暂停");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_NEXT:
                    Logger.d("++下一首");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_LYRICS:
                    Logger.d("++显示歌词");
                    break;
                case Constant.ACTION_CUSTOM_VIEW_OPTIONS_CANCEL:
                    mNM.cancel(Notifications.NOTIFICATION_CUSTOM);
                    break;
                case Constant.ACTION_BIG_TEXT_STYLE:
                    Logger.d("====大文字样式通知");
                    break;
                default:
                    break;
            }
        }
        return START_STICKY;
    }

    private void dealWithActionMusic(Intent intent) {
        int opt = intent.getIntExtra(Constant.ACTION_MUSIC_OPTIONS, -1);
        Logger.d("++ opt = " + opt);
        switch (opt) {
            case Constant.ACTION_MUSIC_PLAY_OR_PAUSE:
                Logger.d("++播放/暂停");
                break;
            case Constant.ACTION_MUSIC_PRE:
                Logger.d("++上一首");
                break;
            case Constant.ACTION_MUSIC_NEXT:
                Logger.d("++下一首");
                break;
            case Constant.ACTION_MUSIC_LOVE:
                Logger.d("++喜欢");
                break;
            case Constant.ACTION_MUSIC_LYRICS:
                Logger.d("++显示歌词");
                break;
            case Constant.ACTION_MUSIC_CLOSE:
                Logger.d("++关闭");
                mNM.cancel(Notifications.NOTIFICATION_CUSTOM);
                break;
            default:
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    private void dealWithActionReplay(Intent intent) {
        Bundle result = RemoteInput.getResultsFromIntent(intent);
        if(result != null) {
            String content = result.getString(Constant.REMOTE_INPUT_RESULT_KEY);
            Log.i(TAG,"content = " + content);
            mNM.cancel(Notifications.NOTIFICATION_REMOTE_INPUT);
        }
    }

    private void dealWithActionSendProgressNotification() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0 ; i<=100 ; i++) {
                    Notifications.sendProgressViewNotification(NotificationService.this, mNM, i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void dealWithActionCustomHeadsUpView(Intent intent) {
        String headsUpOption = intent.getStringExtra(Constant.EXTRA_OPTIONS);
        Logger.i(TAG,"heads up option = " + headsUpOption);
        if(headsUpOption == null) {
            return;
        }
        switch (headsUpOption) {
            case Constant.ACTION_ANSWER:
                Logger.d("接听");
                mNM.cancel(Notifications.NOTIFICATION_CUSTOM_HEADS_UP);
                break;
            case Constant.ACTION_REJECT:
                Logger.d("挂断");
                mNM.cancel(Notifications.NOTIFICATION_CUSTOM_HEADS_UP);
                break;
            default:
                break;
        }
    }
}
